/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.ball.views.obstacles
{
	import com.puny.ball.utils.CollisionPL;
	import com.puny.ball.utils.Vector2D;
	
	public class RotatingBoxObstacle extends BoxShapeObstacle
	{
		private const RADIAN:Number = Math.PI / 180;
		private var _inflation:Number = 0;;	
		private var _w:Number = 0;
//		private const _rw:Vector2D = new Vector2D();
//		private const _v:Point = new Point();
		private var _c_1:Number = 0;
		private var _s:Number = 0;
		private const _rw:Vector2D = new Vector2D();
		private const _dif:Vector2D = new Vector2D();
		private const _l:Vector2D = new Vector2D();
		private const _a_dir:Vector2D = new Vector2D();
		private const _a_invert:Vector2D = new Vector2D();
		public function RotatingBoxObstacle()
		{
			super();
		}
		
		override public function inflate(r:Number):void {
			super.inflate(r);
			_inflation = r;
		}
		
		public function setRectangle(w:Number, h:Number):void {
			graphics.beginFill(0xFF0000,0);
			graphics.drawRect(0, -h/2, w, h/2);
			graphics.endFill();
		}
		
		override public function set rotation(value:Number):void {
			_w = (value - super.rotation) * RADIAN;
			_c_1 = Math.cos(_w) - 1;
			_s = Math.sin(_w);
			super.rotation = value;
			inflate(_inflation);
		}
		
		override public function collision(org:Vector2D, dir:Vector2D):Boolean {
			if (_w == 0) {
				_rw.x = 0;
				_rw.y = 0;
				return super.collision(org, dir);
			}

			//add swept length into dir.
			var z:Number;
			var cpl:CollisionPL = checkers[0][0];
//			_l.x = org.x - cpl.r.x;
//			_l.y = org.y - cpl.r.y;
			z = 2;//_l.length / cpl.dir.length;
			_l.x = cpl.dir.x * (z > 1 ? 1 : z);
			_l.y = cpl.dir.y * (z > 1 ? 1 : z);
//			trace("dir,z",_l, z);
			
			_rw.x = _c_1 * _l.x - _s * _l.y;
			_rw.y = _s * _l.x + _c_1 * _l.y;
//			trace("_rw dot dir=",_rw.dot(dir));

			_a_dir.x = dir.x - _rw.x;
			_a_dir.y = dir.y - _rw.y;
			_dif.x = (dir.x + org.x) - (cpl.dir.x + cpl.r.x);
			_dif.y = (dir.y + org.y) - (cpl.dir.y + cpl.r.y);
//			trace("dot",_rw.dot(_dif)/_rw.length/_dif.length,_rw,_dif);
			if (_rw.length != 0 && _dif.length != 0) {
				if (_rw.dot(_dif)/_rw.length/_dif.length>=0|| dir.length == 0) {
					return super.collision(org, _a_dir);
				}
			}

//			trace("dir=original");
			return super.collision(org,dir);
		}
		
		override protected function collisionWithLine(checker:CollisionPL, pt:Vector2D):Boolean {
			if (super.collisionWithLine(checker, pt)) {
				_a_invert.x = newPos.x - mirroredPos.x;
				_a_invert.y = newPos.y - mirroredPos.y;
				if (_a_invert.dot(_rw)<0) {
					newPos.x -= _rw.x;
					newPos.y -= _rw.y;
				} else {
					newPos.x += _rw.x;
					newPos.y += _rw.y;
				}
				return true;
			}
			return false;
		}
	}
}